import api from './auth.js'

// 后台管理API
export const adminAPI = {
  // 仪表盘数据
  getDashboardStats: () => {
    return api.get('/admin/dashboard/stats')
  },

  getRecentOrders: () => {
    return api.get('/admin/dashboard/recent-orders')
  },

  getPopularBooks: () => {
    return api.get('/admin/dashboard/popular-books')
  },

  getSalesTrend: () => {
    return api.get('/admin/dashboard/sales-trend')
  },

  getCategorySales: () => {
    return api.get('/admin/dashboard/category-sales')
  },

  // 用户管理
  getUserList: (params) => {
    return api.get('/admin/users', { params })
  },

  createUser: (userData) => {
    return api.post('/admin/users', userData)
  },

  updateUser: (userId, userData) => {
    return api.put(`/admin/users/${userId}`, userData)
  },

  deleteUser: (userId) => {
    return api.delete(`/admin/users/${userId}`)
  },

  toggleUserStatus: (userId, status) => {
    return api.patch(`/admin/users/${userId}/status`, { status })
  },

  // 图书管理
  getBookList: (params) => {
    return api.get('/admin/books', { params })
  },

  createBook: (bookData) => {
    return api.post('/admin/books', bookData)
  },

  updateBook: (bookId, bookData) => {
    return api.put(`/admin/books/${bookId}`, bookData)
  },

  deleteBook: (bookId) => {
    return api.delete(`/admin/books/${bookId}`)
  },

  toggleBookStatus: (bookId, status) => {
    return api.patch(`/admin/books/${bookId}/status`, { status })
  },

  // 分类管理
  getCategoryList: (params) => {
    return api.get('/admin/categories', { params })
  },

  createCategory: (categoryData) => {
    return api.post('/admin/categories', categoryData)
  },

  updateCategory: (categoryId, categoryData) => {
    return api.put(`/admin/categories/${categoryId}`, categoryData)
  },

  deleteCategory: (categoryId) => {
    return api.delete(`/admin/categories/${categoryId}`)
  },

  toggleCategoryStatus: (categoryId, status) => {
    return api.patch(`/admin/categories/${categoryId}/status`, { status })
  },

  // 订单管理
  getOrderList: (params) => {
    return api.get('/admin/orders', { params })
  },

  getOrderDetail: (orderId) => {
    return api.get(`/admin/orders/${orderId}`)
  },

  updateOrderStatus: (orderId, status) => {
    return api.patch(`/admin/orders/${orderId}/status`, { status })
  },

  shipOrder: (orderId, shippingData) => {
    return api.post(`/admin/orders/${orderId}/ship`, shippingData)
  },

  cancelOrder: (orderId, reason) => {
    return api.post(`/admin/orders/${orderId}/cancel`, { reason })
  },

  // 新闻管理
  getNewsList: (params) => {
    return api.get('/admin/news', { params })
  },

  createNews: (newsData) => {
    return api.post('/admin/news', newsData)
  },

  updateNews: (newsId, newsData) => {
    return api.put(`/admin/news/${newsId}`, newsData)
  },

  deleteNews: (newsId) => {
    return api.delete(`/admin/news/${newsId}`)
  },

  toggleNewsStatus: (newsId, status) => {
    return api.patch(`/admin/news/${newsId}/status`, { status })
  },

  toggleNewsRecommended: (newsId, isRecommended) => {
    return api.patch(`/admin/news/${newsId}/recommended`, { isRecommended })
  },

  // 获取新闻分类列表
  getNewsCategories: () => {
    return api.get('/news/categories')
  },

  // 创建新闻分类
  createNewsCategory: (categoryData) => {
    return api.post('/admin/news/categories', categoryData)
  },

  // 评论管理
  getReviewList: (params) => {
    return api.get('/admin/reviews', { params })
  },

  getReviewDetail: (reviewId) => {
    return api.get(`/admin/reviews/${reviewId}`)
  },

  updateReview: (reviewId, reviewData) => {
    return api.put(`/admin/reviews/${reviewId}`, reviewData)
  },

  deleteReview: (reviewId) => {
    return api.delete(`/admin/reviews/${reviewId}`)
  },

  toggleReviewStatus: (reviewId, status) => {
    return api.patch(`/admin/reviews/${reviewId}/status`, { status })
  },

  // 文件上传
  uploadFile: (file, type = 'image') => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('type', type)
    
    return api.post('/admin/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 批量操作
  batchDeleteUsers: (userIds) => {
    return api.delete('/admin/users/batch', { data: { ids: userIds } })
  },

  batchDeleteBooks: (bookIds) => {
    return api.delete('/admin/books/batch', { data: { ids: bookIds } })
  },

  batchDeleteNews: (newsIds) => {
    return api.delete('/admin/news/batch', { data: { ids: newsIds } })
  },

  batchDeleteReviews: (reviewIds) => {
    return api.delete('/admin/reviews/batch', { data: { ids: reviewIds } })
  },

  // 数据导出
  exportUsers: (params) => {
    return api.get('/admin/export/users', { 
      params,
      responseType: 'blob'
    })
  },

  exportBooks: (params) => {
    return api.get('/admin/export/books', { 
      params,
      responseType: 'blob'
    })
  },

  exportOrders: (params) => {
    return api.get('/admin/export/orders', { 
      params,
      responseType: 'blob'
    })
  },

  // 系统设置
  getSystemSettings: () => {
    return api.get('/admin/settings')
  },

  updateSystemSettings: (settings) => {
    return api.put('/admin/settings', settings)
  },

  // 操作日志
  getOperationLogs: (params) => {
    return api.get('/admin/logs', { params })
  },

  // 畅销管理
  getBestsellerList: (params) => {
    return api.get('/admin/bestsellers', { params })
  },

  createBestseller: (bestsellerData) => {
    return api.post('/admin/bestsellers', bestsellerData)
  },

  updateBestseller: (bestsellerId, bestsellerData) => {
    return api.put(`/admin/bestsellers/${bestsellerId}`, bestsellerData)
  },

  deleteBestseller: (bestsellerId) => {
    return api.delete(`/admin/bestsellers/${bestsellerId}`)
  },

  updateBestsellerRanks: (rankUpdates) => {
    return api.patch('/admin/bestsellers/ranks', { updates: rankUpdates })
  },

  getAvailableBooks: (params) => {
    return api.get('/admin/bestsellers/available-books', { params })
  },

  // 生成畅销榜单
  generateBestsellerRankings: () => {
    return api.get('/admin/bestsellers/generate')
  },

  // 管理员个人资料和设置
  getAdminProfile: () => {
    return api.get('/admin/profile')
  },

  updateAdminProfile: (profileData) => {
    return api.put('/admin/profile', profileData)
  },

  changePassword: (passwordData) => {
    return api.put('/admin/password', passwordData)
  },

  getAdminStats: () => {
    return api.get('/admin/profile/stats')
  },

  uploadAvatar: (formData) => {
    return api.post('/admin/profile/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  updatePreferences: (preferences) => {
    return api.put('/admin/preferences', preferences)
  },

  updateNotifications: (notifications) => {
    return api.put('/admin/notifications', notifications)
  },

  updateSystemConfig: (config) => {
    return api.put('/admin/system-config', config)
  },

  clearCache: (type) => {
    return api.delete(`/admin/cache/${type}`)
  },

  createBackup: () => {
    return api.post('/admin/backup')
  },

  // 标签管理
  getAllTags: () => {
    return api.get('/admin/tags/all')
  }
}

export default adminAPI
